// @formatter:off
/**
 * @file c.tare.js
 * @author Alejandro Dario Simi
 * @date $Date: 2013-05-27 04:56:35 +0000 (Mon, 27 May 2013) $
 *
 * $Id: c.tare.js 67 2013-05-27 04:56:35Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0/extensions/TagsRelator/templates/c.tare.js $
 */
// @formatter:on

var TARE_autoTransID = 0;

function TARE_CreateAction(form) {
	var ok = true;

	if(ok && $("#ACTION_NAME").val() == "") {
		alert("You must enter a name for the new action");
		$("#ACTION_NAME").focus();
		ok = false;
	}
	if(ok && $("#ACTION_MODE").val() == "") {
		alert("You must select a TagsRelator mode");
		$("#ACTION_MODE").focus();
		ok = false;
	}

	return ok;
}

function TARE_Cancel(rule) {
	TARE_Done(rule);
}

function TARE_Done(rule) {
	document.location.href = "?action=<:VAR:/>WC_ACTION_RULE_SHOW<::/>&group=<:VAR:/>GROUP-ID<::/>&rule=" + rule;
}

function TARE_loadSelectableTags() {
	$(".TARE_SelectableTags").bind("keydown", function(event) {
		if(event.keyCode === $.ui.keyCode.TAB && $(this).data("autocomplete").menu.active) {
			event.preventDefault();
		}
	}).autocomplete({
		source: function(request, response) {
			var pattern = request.term.split(/,\s*/).pop();
			if(pattern.length == 0) {
				return false;
			}

			console.log("loadSelectableTags(): Asking for pattern '" + pattern + "'");

			TARE_autoTransID++;
			console.log("loadSelectableTags(): Sending transaction '" + TARE_autoTransID + "'");
			jQuery.get("service.php?action=<:VAR:/>WC_SERVICE_TAGS<::/>&group=<:VAR:/>GROUP-ID<::/>&pattern=" + pattern + "&transaction=" + TARE_autoTransID, function(data) {
				var xml = $($.parseXML(data));
				var transId = xml.find("wc-transaction").text();
				console.log("loadSelectableTags(): Receiving transaction '" + transId + "'");
				if(transId == TARE_autoTransID) {
					console.log("loadSelectableTags(): Setting results from transaction '" + transId + "'");
					if(xml.find("wc-success").length) {
						var availableTags = Array();

						xml.find("wc-result>tag>name").each(function() {
							availableTags.push($(this).text());
						});
						console.log("loadSelectableTags(): tags received: '" + availableTags.length + "'");

						response(availableTags);
					} else if(xml.find("wc-failed").length) {
						//var	error = "";
						//if(xml.find("wc-error>wc-code").length) {
						//	error = "[" + xml.find("wc-error>wc-code").text() + "] ";
						//}
						//error = error + xml.find("wc-error>wc-message").text();

						//$("#EDITTAGS_LIST").text("Failed: " + error);
					}
				}
			});
		},
		focus: function() {
			// prevent value inserted on focus
			return false;
		},
		select: function(event, ui) {
			var terms = this.value.split(/,\s*/);
			// remove the current input
			terms.pop();
			// add the selected item
			terms.push(ui.item.value);
			// add placeholder to get the comma-and-space at the end
			terms.push("");
			this.value = terms.join(", ");
			return false;
		}
	});
}

$(function() {
	TARE_loadSelectableTags();
});
